<template>
	<view class="settings-container">
		<!-- 阅读设置 -->
		<view class="settings-group">
			<view class="group-title">阅读设置</view>
			<view class="settings-list">
				<view class="settings-item">
					<text class="item-label">字体大小</text>
					<view class="item-value">
						<text>{{ fontSize }}px</text>
						<text class="iconfont icon-arrow-right"></text>
					</view>
				</view>
				<view class="settings-item">
					<text class="item-label">阅读背景</text>
					<view class="item-value">
						<view class="theme-preview" :style="{ background: currentTheme.background }"></view>
						<text class="iconfont icon-arrow-right"></text>
					</view>
				</view>
				<view class="settings-item">
					<text class="item-label">自动翻页</text>
					<switch :checked="autoPage" @change="toggleAutoPage" color="#3cc51f" />
				</view>
				<view class="settings-item">
					<text class="item-label">翻页动画</text>
					<switch :checked="pageAnimation" @change="togglePageAnimation" color="#3cc51f" />
				</view>
			</view>
		</view>
		
		<!-- 缓存设置 -->
		<view class="settings-group">
			<view class="group-title">缓存设置</view>
			<view class="settings-list">
				<view class="settings-item">
					<text class="item-label">缓存大小</text>
					<view class="item-value">
						<text>{{ cacheSize }}</text>
						<text class="iconfont icon-arrow-right"></text>
					</view>
				</view>
				<view class="settings-item" @tap="clearCache">
					<text class="item-label">清除缓存</text>
					<view class="item-value">
						<text class="iconfont icon-arrow-right"></text>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 通知设置 -->
		<view class="settings-group">
			<view class="group-title">通知设置</view>
			<view class="settings-list">
				<view class="settings-item">
					<text class="item-label">书籍更新提醒</text>
					<switch :checked="bookUpdateNotify" @change="toggleBookUpdateNotify" color="#3cc51f" />
				</view>
				<view class="settings-item">
					<text class="item-label">每日阅读提醒</text>
					<switch :checked="dailyReadNotify" @change="toggleDailyReadNotify" color="#3cc51f" />
				</view>
			</view>
		</view>
		
		<!-- 其他设置 -->
		<view class="settings-group">
			<view class="group-title">其他设置</view>
			<view class="settings-list">
				<view class="settings-item" @tap="checkUpdate">
					<text class="item-label">检查更新</text>
					<view class="item-value">
						<text class="version">当前版本 {{ appVersion }}</text>
						<text class="iconfont icon-arrow-right"></text>
					</view>
				</view>
				<view class="settings-item" @tap="goToPrivacy">
					<text class="item-label">隐私政策</text>
					<view class="item-value">
						<text class="iconfont icon-arrow-right"></text>
					</view>
				</view>
				<view class="settings-item" @tap="goToAbout">
					<text class="item-label">关于我们</text>
					<view class="item-value">
						<text class="iconfont icon-arrow-right"></text>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 退出登录 -->
		<view class="logout-btn" @tap="handleLogout">退出登录</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			fontSize: 18,
			currentTheme: {
				name: '默认',
				background: '#ffffff'
			},
			autoPage: false,
			pageAnimation: true,
			cacheSize: '23.5MB',
			bookUpdateNotify: true,
			dailyReadNotify: false,
			appVersion: '1.0.0'
		}
	},
	methods: {
		toggleAutoPage(e) {
			this.autoPage = e.detail.value
			// 保存设置
			this.saveSettings()
		},
		togglePageAnimation(e) {
			this.pageAnimation = e.detail.value
			this.saveSettings()
		},
		toggleBookUpdateNotify(e) {
			this.bookUpdateNotify = e.detail.value
			this.saveSettings()
		},
		toggleDailyReadNotify(e) {
			this.dailyReadNotify = e.detail.value
			this.saveSettings()
		},
		clearCache() {
			uni.showModal({
				title: '提示',
				content: '确定要清除缓存吗？',
				success: (res) => {
					if (res.confirm) {
						uni.showLoading({
							title: '清理中...'
						})
						
						setTimeout(() => {
							uni.hideLoading()
							this.cacheSize = '0B'
							uni.showToast({
								title: '清理完成',
								icon: 'success'
							})
						}, 1500)
					}
				}
			})
		},
		checkUpdate() {
			uni.showLoading({
				title: '检查更新中...'
			})
			
			setTimeout(() => {
				uni.hideLoading()
				uni.showToast({
					title: '已是最新版本',
					icon: 'none'
				})
			}, 1500)
		},
		goToPrivacy() {
			uni.navigateTo({
				url: '/pages/privacy/privacy'
			})
		},
		goToAbout() {
			uni.navigateTo({
				url: '/pages/about/about'
			})
		},
		handleLogout() {
			uni.showModal({
				title: '提示',
				content: '确定要退出登录吗？',
				success: (res) => {
					if (res.confirm) {
						// 清除登录状态
						uni.clearStorageSync()
						// 返回登录页
						uni.reLaunch({
							url: '/pages/login/login'
						})
					}
				}
			})
		},
		saveSettings() {
			// 保存设置到本地存储
			const settings = {
				fontSize: this.fontSize,
				currentTheme: this.currentTheme,
				autoPage: this.autoPage,
				pageAnimation: this.pageAnimation,
				bookUpdateNotify: this.bookUpdateNotify,
				dailyReadNotify: this.dailyReadNotify
			}
			uni.setStorageSync('readSettings', settings)
		}
	},
	onLoad() {
		// 加载本地存储的设置
		const settings = uni.getStorageSync('readSettings')
		if (settings) {
			Object.assign(this, settings)
		}
	}
}
</script>

<style lang="scss">
.settings-container {
	min-height: 100vh;
	background: #f5f5f5;
	padding-bottom: 40rpx;
	
	.settings-group {
		margin-top: 20rpx;
		background: #fff;
		
		.group-title {
			padding: 20rpx 30rpx;
			font-size: 28rpx;
			color: #999;
		}
		
		.settings-list {
			.settings-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 30rpx;
				border-bottom: 1rpx solid #f5f5f5;
				
				&:last-child {
					border-bottom: none;
				}
				
				.item-label {
					font-size: 28rpx;
					color: #333;
				}
				
				.item-value {
					display: flex;
					align-items: center;
					
					.theme-preview {
						width: 40rpx;
						height: 40rpx;
						border-radius: 6rpx;
						border: 1rpx solid #ddd;
						margin-right: 20rpx;
					}
					
					.version {
						font-size: 24rpx;
						color: #999;
						margin-right: 20rpx;
					}
					
					.iconfont {
						font-size: 32rpx;
						color: #999;
					}
					
					text {
						font-size: 28rpx;
						color: #666;
						margin-right: 20rpx;
					}
				}
			}
		}
	}
	
	.logout-btn {
		margin: 60rpx 30rpx;
		height: 88rpx;
		line-height: 88rpx;
		text-align: center;
		background: #ff4444;
		color: #fff;
		font-size: 32rpx;
		border-radius: 44rpx;
	}
}
</style> 